
package com.matthewtamlin.fortytwo.library_tests;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.StackLayout;

import static ohos.agp.components.Component.HIDE;
import static ohos.agp.components.Component.VISIBLE;

/**
 * A TestHarness which displays control buttons on top of the test view.
 *
 * @param <V> the type of view being tested
 */
public abstract class ControlsOverViewTestHarness<V> extends Ability {
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_controlsoverview);
        getTestViewContainer().addComponent((Component) getTestView());
        initialiseControlHiding();
    }

    public Component getRootView() {
        return findComponentById(ResourceTable.Id_controlsOverView_root);
    }

    public DirectionalLayout getControlsContainer() {
        return (DirectionalLayout) findComponentById(ResourceTable.Id_controlsOverView_controlsContainer);
    }

    public StackLayout getTestViewContainer() {
        return (StackLayout) findComponentById(ResourceTable.Id_controlsOverView_testViewContainer);
    }

    /**
     * getTestView
     *
     * @return the view under test, not null
     */
    public abstract V getTestView();

    /**
     * Configures a button to hide/show the controls when clicked.
     */
    private void initialiseControlHiding() {
        final Button toggleControlVisibilityButton = (Button) findComponentById(ResourceTable.Id_controlsOverView_showHideControlsButton);
        final DirectionalLayout controlButtonContainer = (DirectionalLayout) findComponentById(ResourceTable.Id_controlsOverView_controlsContainer);
        toggleControlVisibilityButton.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                controlButtonContainer.setVisibility(controlButtonContainer.getVisibility()
                    == VISIBLE ? HIDE : VISIBLE);
            }
        });
    }
}
